<?php
/**
 * 用户类
 * @copyright 许雷
 * @since v1.0,
 * */ 
class RandUser{

} 
 
class myweixin{
    public static function goLogin($callbackUrl=''){
        setcookie('QI_VIEW_ID',g('randId'),time()+3600*24*365*10);//保存回来的时候要看的页面
    
        if(!$callbackUrl) $callbackUrl = 'http://'.$_SERVER['HTTP_HOST'].'/'.COOKIE_DIR.'/index,callback';
        if(IS_LINUX){
                $redirectUrl = 'http://www.qilaiwan.com/login/?to='.base64_encode($callbackUrl);
                $encodeUrl  = urlencode($redirectUrl);
                $weixinUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.WEIXIN_FUWUHAO_ID.'&redirect_uri='.$encodeUrl.'&response_type=code&scope='.WEIXIN_SCOPE.'&state=default#wechat_redirect';	
        }else{//windows 模拟测试
                $redirectUrl = 'http://local.www.qilaiwan.com/login/?to='.base64_encode($callbackUrl);
                $encodeUrl  = urlencode($redirectUrl);
                $weixinUrl =  'http://local.www.qilaiwan.com/login/index,getCode?appid='.WEIXIN_FUWUHAO_ID.'&redirect_uri='.$encodeUrl.'&response_type=code&scope='.WEIXIN_SCOPE.'&state=default#wechat_redirect';	
        }
        myresponse::redirect($weixinUrl,0,'请授权微信登录');
    }
    
    //是否微信登录过
    public static function isLogin(){
        $randId =c('QI_RID');
        if(c('QI_LOGIN_KEY')==md5($randId.COOKIE_KEY2)) return true;
        return false;
    }
    
    //通过code获取openid和token
    public static function getTokenByCode($code){
         require_once(LIB_PATH.'spider/myspider.php');
		$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.WEIXIN_FUWUHAO_ID.'&secret='.WEIXIN_FUWUHAO_SECRET.'&code='.$code.'&grant_type=authorization_code';
        
		$result = myspider::fetch($url);
		$json   = json_decode($result);
        
        if('' == trim($json->openid)){//返回的openId不对
			mylogin::logout();
			myresponse::redirect('index,default',3,'请您授权微信登录');
		}
        
        return $json;
    }
    
    public static function getInfoByToken($token,$openId){
            require_once(LIB_PATH.'spider/myspider.php');
        	$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openId.'&lang=zh_CN'; 
            $result = myspider::fetch($url);
            $userInfo   = json_decode($result);
            $userInfo->headimgurl = preg_replace("/\d+$/","46",$userInfo->headimgurl);
            
            return $userInfo;
    }
    
    public static function getUserInfo($code){
        if(!IS_LINUX) {
            $randUserInfo = new RandUser();
            $randUserInfo->nickname = 'randUser'.date('mdHis');
            $randUserInfo->openid = 'randOpenId'.date('mdHis');
            $randUserInfo->headimgurl = 'http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46';
            return  $randUserInfo; 
        } 
    
		$json = myweixin::getTokenByCode($code);
		$token  = $json->access_token; 
		$openId = $json->openid;
        return myweixin::getInfoByToken($token,$openId);
    }
 
}
?>